<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/10
 * Time: 17:26
 */

namespace App\Services;

use App\Models\Goods;
use App\Models\GoodsAttribute;

class GoodsService extends BaseService
{
    protected $modelClass = Goods::class;

    /**
     * 添加
     * @param $postData
     * @return bool|string
     * @throws \Exception
     */
    public function add($postData)
    {
//        $postData['uuid'] = uuid();
//        $postData['cover_img'] = '';
//        $postData['cover_img'] = '';
//        $postData['cover_img'] = '';
//        $postData['cover_img'] = '';
//        $postData['cover_img'] = '';
//        return $this->saveData($postData);


         $obj = $this->detail('a1812b10-e400-11e8-8e75-c94da1a72666');

        return $obj;
    }

    /**
     * 编辑
     * @param $uuid
     * @param $postData
     * @return mixed
     * @throws \Exception
     */
    public function edit($uuid, $postData)
    {
        try {
            DB::transaction(function()use($uuid, $postData){
                $obj = $this->findByUuidOrFail($uuid);
                $obj->update(filter_null($postData));

                if (!empty($postData['input_attribute']) || !empty($postData['check_attribute']) || !empty($postData['price_attribute'])) {
                    $attData = $attributeData = $attributeAttachData = $skuData = [];
                    foreach ($postData['input_attribute'] as $key => $val) {
                        foreach ($val as $v) {
                            $attData[] = [
                                'name' => $key,
                                'value' => $v,
                            ];
                        }
                    }
                    foreach ($postData['check_attribute'] as $key => $val) {
                        foreach ($val as $v) {
                            $attData[] = [
                                'name' => $key,
                                'value' => $v,
                            ];
                        }
                    }
                    foreach ($postData['price_attribute'] as $key => $val) {
                        foreach ($val as $v) {
                            $attData[] = [
                                'name' => $key,
                                'value' => $v,
                            ];
                        }
                    }







                    foreach ($postData['attribute'] as $a) {
                        $attUuid = $a['uuid']?:uuid();
                        $attributeData[$a['attribute_uuid']] = [
                            'name' => $a['name'],
                            'value' => $a['value'],
                            'uuid' => $attUuid
                        ];

                        if (!empty($a['attach'])) {
                            $attributeAttachData[$a['attach'][0]['uuid']] = [
                                'relation_uuid' => $attUuid,
                                'target_type' => GoodsAttribute::class,
                            ];
                        }
                    }
                    $obj->attribute()->sync($attributeData);
                    $obj->attribute()->attach()->sync($attributeAttachData);

                    foreach ($postData['sku'] as $s) {
                        $skuData[] = [];
                    }
                }

            });

        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 详情
     * @param $uuid
     * @return mixed
     * @throws \Exception
     */
    public function detail($uuid)
    {
        try {
            return $this->model
                ->with(['category', 'brand', 'sku', 'attach', 'attribute.attach'])
                ->where('uuid', $uuid)
                ->first();
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 删除
     * @param $uuid
     * @return mixed
     * @throws \Exception
     */
    public function remove($uuid)
    {
        try {
            return $this->removeByUuid($uuid);
        } catch (\Exception $e) {
            throw new \Exception('删除失败！');
        }
    }

    /**
     * @desc 列表
     * @param array $search
     * @param int $per_page
     * @return array
     */
    public function getList($search=[], $per_page=20)
    {
        $columns = ['*'];

        $model = $this->searcher($this->model, $search);

        $conf = [
            'search'  => 'conf_search.goods_getList',
            'columns' => 'conf_columns.goods_getList',
            'buttons' => 'conf_buttons.goods_getList',
        ];
        return $this->get_paginate($model->paginate($per_page, $columns), $conf);
    }
}
